package com.ljfiction.book.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.OpenBean;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.GonggaoActivity;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by admin on 2017/9/22.
 */

public class MyprivateletterFragment extends BaseFragment {
    private BaseQuickAdapter mAdapter;
    private Context context;
    private RecyclerView myshupingrecycler;
    private SmartRefreshLayout refreshMyshupingrecycle;
    private int page = 1;
    private OpenBean openBean;
    private LinearLayout layout;
    private List<OpenBean.DataBean.ListBean> datas = new ArrayList<>();
    private NoBackgLodDialog noBackgLodDialog;
    private boolean isfirst = true;
    private Intent intent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
        intData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.myshuping_recycaler, null);
        myshupingrecycler = view.findViewById(R.id.myshupingrecycler);
        refreshMyshupingrecycle = view.findViewById(R.id.refresh_myshupingrecycle);
        layout = view.findViewById(R.id.zanwushuju);
        intView();
        return view;
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/sys-getsystemmsg")

                .params("startpage", String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (isfirst) {
                            if (noBackgLodDialog == null) {
                                noBackgLodDialog = new NoBackgLodDialog(context, false);
                            }
                            noBackgLodDialog.show();
                        }
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (isfirst) {
                            noBackgLodDialog.dismis();
                            isfirst = false;
                        }
                        refreshMyshupingrecycle.finishRefresh();
                        refreshMyshupingrecycle.finishLoadMore();
                        Gson gson = new Gson();
                        try {
                            openBean = gson.fromJson(s, OpenBean.class);
                            if (openBean.getReturnCode() == 200) {
                                datas.addAll(openBean.getData().getList());
                                mAdapter.notifyDataSetChanged();
                                if (datas.size() < 1) {
                                    layout.setVisibility(View.VISIBLE);
                                } else {
                                    layout.setVisibility(View.GONE);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        myshupingrecycler.setAdapter(mAdapter = new BaseQuickAdapter<OpenBean.DataBean.ListBean, BaseViewHolder>(R.layout.tongzhi, datas) {
            @Override
            protected void convert(BaseViewHolder helper, OpenBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.titile_tongzhi, item.getTitle())
                        .setText(R.id.time_tongzhi, TimeTool.getTimeByLong(item.getDateTimes()))
                        .setText(R.id.context_tongzhi, item.getMessageContent());
            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            intent = new Intent(context, GonggaoActivity.class);
            intent.putExtra("title", datas.get(position).getTitle());
            intent.putExtra("text", datas.get(position).getMessageContent());
            startActivity(intent);
        });
        myshupingrecycler.setLayoutManager(new LinearLayoutManager(context));
        // myshupingrecycler.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        refreshMyshupingrecycle.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (openBean.getData().isIsLastPage()) {
                    refreshMyshupingrecycle.finishLoadMore();
                    refreshMyshupingrecycle.setEnableLoadMore(false);
                } else {
                    page++;
                    intData();
                }

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                refreshMyshupingrecycle.setEnableLoadMore(true);
                intData();
            }
        });
    }
}
